import { createRouter, createWebHistory } from "vue-router";
import Layout from "@/layout/index.vue";
import { useUserStore } from "@/stores/user";
import R from "@/views/Residents/index.vue";
// 公共路由
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    hidden: true,
  },

  {
    path: "/user",
    component: R,
    children: [
      {
        path: "event/list",
        name: "rEventList",
        component: () => import("@/views/event/list.vue"),
        meta: { title: "活动管理", icon: "List" },
      },
      {
        path: "event/detail/:id",
        name: "rEventDetail",
        component: () => import("@/views/event/detail.vue"),
        meta: { title: "活动详情" },
        hidden: true,
      },
      {
        path: "complaint/list",
        name: "rComplaintList",
        component: () => import("@/views/complaint/list.vue"),
        meta: { title: "投诉管理", icon: "List" },
      },
      {
        path: "property-fee/list",
        name: "rPropertyFeeList",
        component: () => import("@/views/property/fee/list.vue"),
        meta: { title: "物业费管理", icon: "Money" },
      },
      {
        path: "property-fee/detail/:id",
        name: "rPropertyFeeDetail",
        component: () => import("@/views/property/fee/detail.vue"),
        meta: { title: "物业费详情" },
        hidden: true,
      },
      {
        path: "property-fee/add",
        name: "rPropertyFeeAdd",
        component: () => import("@/views/property/fee/form.vue"),
        meta: { title: "添加物业费" },
        hidden: true,
      },
      {
        path: "property-fee/edit/:id",
        name: "rPropertyFeeEdit",
        component: () => import("@/views/property/fee/form.vue"),
        meta: { title: "编辑物业费" },
        hidden: true,
      },
    ],
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        meta: { title: "数据看板", icon: "Histogram" },
      },
      {
        path: "community/list",
        name: "CommunityList",
        component: () => import("@/views/community/index.vue"),
        meta: { title: "小区管理", icon: "House" },
      },
      {
        path: "neighborhood/list",
        name: "NeighborhoodList",
        component: () => import("@/views/neighborhood/index.vue"),
        meta: { title: "居委会管理", icon: "Office" },
      },
      {
        path: "event/list",
        name: "EventList",
        component: () => import("@/views/event/list.vue"),
        meta: { title: "活动管理", icon: "List" },
      },
      {
        path: "event/detail/:id",
        name: "EventDetail",
        component: () => import("@/views/event/detail.vue"),
        meta: { title: "活动详情" },
        hidden: true,
      },
      {
        path: "complaint/list",
        name: "ComplaintList",
        component: () => import("@/views/complaint/list.vue"),
        meta: { title: "投诉管理", icon: "List" },
      },
      {
        path: "complaint/detail/:id",
        name: "ComplaintDetail",
        component: () => import("@/views/complaint/detail.vue"),
        meta: { title: "投诉详情" },
        hidden: true,
      },
      {
        path: "house/list",
        name: "HouseList",
        component: () => import("@/views/house/list.vue"),
        meta: { title: "房屋管理", icon: "HomeFilled" },
      },
      {
        path: "house/detail/:id",
        name: "HouseDetail",
        component: () => import("@/views/house/detail.vue"),
        meta: { title: "房屋详情" },
        hidden: true,
      },
      {
        path: "house/add",
        name: "HouseAdd",
        component: () => import("@/views/house/form.vue"),
        meta: { title: "添加房屋" },
        hidden: true,
      },
      {
        path: "house/edit/:id",
        name: "HouseEdit",
        component: () => import("@/views/house/form.vue"),
        meta: { title: "编辑房屋" },
        hidden: true,
      },
      {
        path: "resident/list",
        name: "ResidentList",
        component: () => import("@/views/resident/list.vue"),
        meta: { title: "居民管理", icon: "UserFilled" },
      },
      {
        path: "resident/detail/:id",
        name: "ResidentDetail",
        component: () => import("@/views/resident/detail.vue"),
        meta: { title: "居民详情" },
        hidden: true,
      },
      {
        path: "resident/add",
        name: "ResidentAdd",
        component: () => import("@/views/resident/form.vue"),
        meta: { title: "添加居民" },
        hidden: true,
      },
      {
        path: "resident/edit/:id",
        name: "ResidentEdit",
        component: () => import("@/views/resident/form.vue"),
        meta: { title: "编辑居民" },
        hidden: true,
      },
      {
        path: "property-fee/list",
        name: "PropertyFeeList",
        component: () => import("@/views/property/fee/list.vue"),
        meta: { title: "物业费管理", icon: "Money" },
      },
      {
        path: "property-fee/detail/:id",
        name: "PropertyFeeDetail",
        component: () => import("@/views/property/fee/detail.vue"),
        meta: { title: "物业费详情" },
        hidden: true,
      },
      {
        path: "property-fee/add",
        name: "PropertyFeeAdd",
        component: () => import("@/views/property/fee/form.vue"),
        meta: { title: "添加物业费" },
        hidden: true,
      },
      {
        path: "property-fee/edit/:id",
        name: "PropertyFeeEdit",
        component: () => import("@/views/property/fee/form.vue"),
        meta: { title: "编辑物业费" },
        hidden: true,
      },
      {
        path: "property-equipment/list",
        name: "PropertyEquipmentList",
        component: () => import("@/views/property/equipment/list.vue"),
        meta: { title: "设备管理", icon: "Tools" },
      },
      {
        path: "property-equipment/detail/:id",
        name: "PropertyEquipmentDetail",
        component: () => import("@/views/property/equipment/detail.vue"),
        meta: { title: "设备详情" },
        hidden: true,
      },
      {
        path: "property-equipment/add",
        name: "PropertyEquipmentAdd",
        component: () => import("@/views/property/equipment/form.vue"),
        meta: { title: "添加设备" },
        hidden: true,
      },
      {
        path: "property-equipment/edit/:id",
        name: "PropertyEquipmentEdit",
        component: () => import("@/views/property/equipment/form.vue"),
        meta: { title: "编辑设备" },
        hidden: true,
      },
    ],
  },
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: constantRoutes,
  // 刷新时滚动到顶部
  scrollBehavior: () => ({ top: 0 }),
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  const token = userStore.token;

  // 如果是访问登录页面，直接放行
  if (to.path === "/login") {
    next();
    return;
  }

  // 需要登录但没有token
  if (!token) {
    next("/login");
    return;
  }

  // // 获取用户信息
  // const userInfoStr = localStorage.getItem("userInfo");
  // if (userInfoStr) {
  //   const userInfo = JSON.parse(userInfoStr);
  //   // 如果是非管理员用户，且访问的不是允许的页面，则强制跳转到活动列表页
  //   if (userInfo.role !== "Admin") {
  //     next("/user");
  //     return;
  //   }
  // }

  next();
});

export default router;
